using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using WcfService1.DTO;

namespace MSIACCSL2.Views.PurchaseReturn
{
    public partial class FrmAdd : Page
    {
        public PurchaseReturnWrapViewModel WrapViewModel
        {
            get
            {
                return this.Resources["POW"] as PurchaseReturnWrapViewModel;
            }
        }

        public PurchaseReturnViewModel ViewModel
        {
            get
            {
                return this.DataContext as PurchaseReturnViewModel;
            }
        }


        public FrmAdd()
        {
            InitializeComponent();

            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as PurchaseReturnWrapViewModel).PurchaseReturnViewModel =
                                        (PurchaseReturnViewModel)this.DataContext;
                               };
      

            this.txtSupplier.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    //ViewModel.GetSupplier(txtSupplier.Text);
                    SupplierSearch supplierSearch = new SupplierSearch();
                    supplierSearch.Closed += new EventHandler(supplierSearch_Closed);
                    supplierSearch.Show();
                    supplierSearch.txtSupplierName.Text = txtSupplier.Text;
                    supplierSearch.txtSupplierName.Focus();
                }
            };

            this.KeyDown += (o, e) =>
            {

                if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.SavePurchaseReturn(ViewModel.PurchaseReturn);
                else if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.Cancel(null);
            };
        }

        void supplierSearch_Closed(object sender, EventArgs e)
        {
            ViewModel.PurchaseReturn.Supplier = (sender as SupplierSearch).Supplier;
            txtSupplier.Focus();
        }

        private void textBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                WrapViewModel.GetInventory(((TextBox)sender).DataContext);
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }


        private void btnCloseStatus_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ViewModel.CloseStatus();
        }
    }
}

